from datetime import datetime

import peewee as orm

from buondua import config

db_proxy = orm.Proxy()

db = orm.SqliteDatabase(
    config.DATABASE_LOCATION,
    pragmas={
        'journal_mode': 'wal',
        'cache_size': -1 * 64000,  # 64MB
        'foreign_keys': 1,
        'ignore_check_constraints': 0,
        'synchronous': 0
    }
)

db_proxy.initialize(db)


class Album(orm.Model):
    ''' Альбом изображений '''
    id = orm.AutoField()
    album_url = orm.TextField()
    title = orm.TextField()
    title_slug = orm.TextField()
    tags = orm.TextField(null=True)
    parsed = orm.BooleanField(default=False)
    telegraph_url = orm.TextField(null=True)
    created_at = orm.DateTimeField(default=datetime.utcnow())
    updated_at = orm.DateTimeField(null=True)

    class Meta:
        database = db_proxy
        table_name = 'album'


class Picture(orm.Model):
    id = orm.AutoField()
    url = orm.TextField()
    pic_num = orm.IntegerField()
    album_id = orm.ForeignKeyField(Album, backref='pictures', on_delete='CASCADE')
    created_at = orm.DateTimeField(default=datetime.utcnow())
    updated_at = orm.DateTimeField(null=True)

    class Meta:
        database = db_proxy
        table_name = 'picture'


MODELS = (Picture, Album)
